<?php
namespace app\api\controller;
use ba\Tree;
use Throwable;
use think\facade\Request;
use app\admin\model\Order as OrderModel;
use app\api\validate\Order as OrderValidate;
use app\common\controller\Api;
class Order extends Api
{	
    /**
     * 订单列表
     * author：mozi
     * time:2025.05.17
     * */
     public function lists(){
          $uid = request()->userId;
          if(isset($uid) && $uid != ''){
               $res = OrderModel::where("uid",$uid)->order("oid desc")->select();
               return json(['code'=>200,'msg'=>'ok','data'=>$res]);      
          }else{
               return json(['code'=>100,'msg'=>'未登录']);      
          }
     }

     /**
     * 获取单个投诉详细
     * author：mozi
     * time:2025.05.17
     * */
     public function details(){
          $id = $this->request->post("id");
          $tel = $this->request->post("tel");
          if($id){
               $res = ComplaintModel::where("id",$id)->where("tel",$tel)->find();
               return json(['code'=>200,'msg'=>'ok','data'=>$res]);      
          }else{
               return json(['code'=>100,'msg'=>'无记录']);      
          }
     }


     /**
     * 投诉回复
     * author：mozi
     * time:2025.05.17
     * */
     public function reply(){
          $id = $this->request->post("id");
          if($id){
               $res = ComplaintReply::where("cid",$id)->order("id desc")->select();
               return json(['code'=>200,'msg'=>'ok','data'=>$res]);      
          }else{
               return json(['code'=>100,'msg'=>'手机号码不能为空']);      
          }
     }


	 /**
     * 用户留言
     * author：mozi
     * time:2025.05.16
     * */
	 public function sub(){
     	   $params = $this->request->post("");
     	   $validate = new MessageValidate();
             try {
                 $validate->check($params);
             } catch (Throwable $e) {
                 return json(['code'=>100,'msg'=>$e->getMessage()]);
             }
             $model = new MessageModel();

             $todayStart = strtotime('today');
             $todayEnd = strtotime('tomorrow') - 1;
             $find  = $model->where('timestamp', '>=', $todayStart)->where('timestamp', '<=', $todayEnd)->where(['ordernum'=>$params['ordernum']])->count();
             if($find){
                  //$this->error("该订单您已留言！");
             }

             $model->startTrans();
             try {

                 $model->save($params);
                 $model->commit();
             } catch (Throwable $e) {
                 $model->rollback();
                 $this->error($e->getMessage());
             }
     		return json(['code'=>200,'msg'=>'ok']);		
	 }
}